#include "Traceur.h"

Traceur::Traceur(){
  nomFichier = "Trace.xml";
  //semPtr = new Semaphore(1);
  xmlDocPtr docOut;
  xmlNodePtr racine;
  docOut = xmlNewDoc(BAD_CAST "1.0");
  racine = xmlNewNode(NULL, BAD_CAST "EVENEMENTS");
  xmlDocSetRootElement(docOut, racine);
  xmlSaveFormatFileEnc(nomFichier.data(), docOut, "UTF-8", 1);

  xmlFreeDoc(docOut);	
}

Traceur::~Traceur(){
 
}
/***************************************************************************************************************************/

void Traceur::demarrageServeur(){
 
  xmlDocPtr doc, docOut;
  xmlNodePtr racine, racineOut;
  xmlNodePtr evenement;
	
  doc = xmlParseFile(nomFichier.data());
  racine = xmlDocGetRootElement(doc);
	
  evenement = xmlNewNode(NULL, BAD_CAST "EVENEMENT");
  xmlNewChild(evenement, NULL, BAD_CAST "TYPE", BAD_CAST "DEMARRAGE_SERVEUR");
  // gestion de la date et l'heure du demarrage de serveur 
  time_t t0; 
  struct tm *date;
  char DateDemarrage[20];
  time(&t0);
  date  = localtime ( &t0 );	  
  strftime ( DateDemarrage,20, "%d%m%Y%H%M%S", date); 	 
  xmlNewChild(evenement, NULL, BAD_CAST "TIME", BAD_CAST DateDemarrage);
  racineOut = xmlCopyNode(racine, 1);
  xmlAddChild(racineOut, evenement);	 
   	
  docOut = xmlNewDoc(BAD_CAST "1.0");
  xmlDocSetRootElement(docOut, racineOut);	

  xmlSaveFormatFileEnc(nomFichier.data(), docOut, "UTF-8", 1);

  xmlFreeDoc(doc);
  xmlFreeDoc(docOut);
	 
}

/***************************************************************************************************************************/

void Traceur::newConnexion(InfoClient  clt){
	
  xmlDocPtr doc, docOut;
  xmlNodePtr racine, racineOut;
  xmlNodePtr evenement;
	
  doc = xmlParseFile(nomFichier.data());
  racine = xmlDocGetRootElement(doc);
	
  evenement = xmlNewNode(NULL, BAD_CAST "EVENEMENT");
  xmlNewChild(evenement, NULL, BAD_CAST "TYPE", BAD_CAST "CONNEXION_CLIENT");
  xmlNewChild(evenement, NULL, BAD_CAST "ADRESSE_IP", BAD_CAST clt.GetIp().data());
  char numScenarioStr[9999];
  sprintf(numScenarioStr, "%d",clt.GetScenarioAttribue());
  xmlNewChild(evenement, NULL, BAD_CAST "NUMERO_TRAVAIL",BAD_CAST numScenarioStr);
  xmlNewChild(evenement, NULL, BAD_CAST "TIME", BAD_CAST clt.GetDerniereConnexion().data());
  racineOut = xmlCopyNode(racine, 1);
  xmlAddChild(racineOut, evenement);	
   	
  docOut = xmlNewDoc(BAD_CAST "1.0");
  xmlDocSetRootElement(docOut, racineOut);	

  xmlSaveFormatFileEnc(nomFichier.data(), docOut, "UTF-8", 1);

  xmlFreeDoc(doc);
  xmlFreeDoc(docOut);
	 
}

/***************************************************************************************************************************/

void Traceur::receptionResulat(InfoClient clt){
 
  xmlDocPtr doc, docOut;
  xmlNodePtr racine, racineOut;
  xmlNodePtr evenement;
	
  doc = xmlParseFile(nomFichier.data());
  racine = xmlDocGetRootElement(doc);
	
  evenement = xmlNewNode(NULL, BAD_CAST "EVENEMENT");
  xmlNewChild(evenement, NULL, BAD_CAST "TYPE", BAD_CAST "RECEPTION_RESULTAT");
  xmlNewChild(evenement, NULL, BAD_CAST "ADRESSE_IP", BAD_CAST clt.GetIp().data());
  // gestion de la date et l'heure du demarrage de serveur 
  time_t t0; 
  struct tm *date;
  char DateDemarrage[20]; 
  time(&t0);
  date  = localtime ( &t0 );	  
  strftime ( DateDemarrage,20, "%d%m%Y%H%M%S", date); 	 
  xmlNewChild(evenement, NULL, BAD_CAST "TIME", BAD_CAST DateDemarrage);
  char numScenarioStr[9999];
  sprintf(numScenarioStr, "%d",clt.GetScenarioAttribue());
  xmlNewChild(evenement, NULL, BAD_CAST "NUMERO_TRAVAIL",BAD_CAST numScenarioStr);
  xmlNewChild(evenement, NULL, BAD_CAST "TPS_CALCUL", BAD_CAST clt.GerTempsCalcul().data());
  racineOut = xmlCopyNode(racine, 1);
  xmlAddChild(racineOut, evenement);	 
   	
  docOut = xmlNewDoc(BAD_CAST "1.0");
  xmlDocSetRootElement(docOut, racineOut);	

  xmlSaveFormatFileEnc(nomFichier.data(), docOut, "UTF-8", 1);

  xmlFreeDoc(doc);
  xmlFreeDoc(docOut);
	 
}
/***************************************************************************************************************************/
void Traceur::clientMort(InfoClient clt){
		 
  xmlDocPtr doc, docOut;
  xmlNodePtr racine, racineOut;
  xmlNodePtr evenement;
	
  doc = xmlParseFile(nomFichier.data());
  racine = xmlDocGetRootElement(doc);
	
  evenement = xmlNewNode(NULL, BAD_CAST "EVENEMENT");
  xmlNewChild(evenement, NULL, BAD_CAST "TYPE", BAD_CAST "CLIENT_MORT");	
  xmlNewChild(evenement, NULL, BAD_CAST "ADRESSE_IP", BAD_CAST clt.GetIp().data());
  char numScenarioStr[9999];
  sprintf(numScenarioStr, "%d",clt.GetScenarioAttribue());
  xmlNewChild(evenement, NULL, BAD_CAST "NUMERO_TRAVAIL",BAD_CAST numScenarioStr);

  time_t t0; 
  struct tm *date;
  char DateMort[20]; 
  time(&t0);
  date  = localtime ( &t0 );	  
  strftime ( DateMort,20, "%d%m%Y%H%M%S", date); 	 
  xmlNewChild(evenement, NULL, BAD_CAST "TIME", BAD_CAST DateMort);
  racineOut = xmlCopyNode(racine, 1);
  xmlAddChild(racineOut, evenement);	
   	
  docOut = xmlNewDoc(BAD_CAST "1.0");
  xmlDocSetRootElement(docOut, racineOut);	

  xmlSaveFormatFileEnc(nomFichier.data(), docOut, "UTF-8", 1);

  xmlFreeDoc(doc);
  xmlFreeDoc(docOut);
}

//*****************************************************************************************************



void Traceur::DeconnexionVeille(InfoClient  clt){
	
  xmlDocPtr doc, docOut;
  xmlNodePtr racine, racineOut;
  xmlNodePtr evenement;
	
  doc = xmlParseFile(nomFichier.data());
  racine = xmlDocGetRootElement(doc);
	
  evenement = xmlNewNode(NULL, BAD_CAST "EVENEMENT");
  xmlNewChild(evenement, NULL, BAD_CAST "TYPE", BAD_CAST "DECONNEXION_CLIENT_VEILLE");
  xmlNewChild(evenement, NULL, BAD_CAST "ADRESSE_IP", BAD_CAST clt.GetIp().data());
  
  time_t t0; 
  struct tm *date;
  char DateConnexion[20];
  time(&t0);
  date  = localtime ( &t0 );
	  
  strftime ( DateConnexion,20, "%d%m%Y%H%M%S", date); 
  xmlNewChild(evenement, NULL, BAD_CAST "TIME", BAD_CAST DateConnexion);
  racineOut = xmlCopyNode(racine, 1);
  xmlAddChild(racineOut, evenement);	
   	
  docOut = xmlNewDoc(BAD_CAST "1.0");
  xmlDocSetRootElement(docOut, racineOut);	

  xmlSaveFormatFileEnc(nomFichier.data(), docOut, "UTF-8", 1);

  xmlFreeDoc(doc);
  xmlFreeDoc(docOut);
	 
}
